package de.cbf.cam.dsa.rcp.model.test.mock;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import de.cbf.cam.dsa.rcp.model.entities.CultureDSA;
import de.cbf.cam.dsa.rcp.model.entities.ProfessionDSA;
import de.cbf.cam.dsa.rcp.model.entities.RaceDSA;
import de.cbf.cam.dsa.rcp.model.entities.StructureData;
import de.cbf.cam.dsa.services.impl.rcp_provider.extension.interfaces.AbstractParameteredPersister;
import de.cbf.cam.dsa.services.impl.rcp_provider.extension.interfaces.IRCPPersisterStructure;
import de.cbf.cam.dsa.services.interfaces.rcp_provider.IServiceDSAProviderRCPStructure;
import de.cbf.util.db.interfaces.exceptions.EntityLoadException;
import de.cbf.util.uuidheader.entities.EntityWeighted;
import de.cbf.util.uuidheader.entities.UUIDReference;
import de.cbf.util.uuidheader.util.EntityWeightedList;

public class MockRCPStructureSupplier extends AbstractParameteredPersister implements IRCPPersisterStructure {
	private static final Log logger = LogFactory.getLog(MockRCPStructureSupplier.class);
	private static final String NAME = "Mock structure supplier";

	protected static MockRCPStructureSupplier instance;

	protected StructureData structureData;

	public MockRCPStructureSupplier() {
		super();
		structureData = new StructureData();

		setName(NAME);

		prepareRCPStructure();
	}

	public static MockRCPStructureSupplier getInstance() {
		if(instance == null){
			instance = new MockRCPStructureSupplier();
		}
		return instance;
	}

	@Override
	public Collection<Object> getKnownLoadParameters() {
		List<Object> result = new ArrayList<Object>();
		return result;
	}
	@Override
	public Collection<Object> getKnownSaveParameters() {
		List<Object> result = new ArrayList<Object>();
		return result;
	}

	@Override
	public StructureData loadStructure(UUID uuid) {
		return structureData;
	}

	@Override
	public Collection<StructureData> loadAllStructureData()
			throws EntityLoadException {
		Collection<StructureData> result = new ArrayList<StructureData>();
		result.add(structureData);
		return result;
	}

	@Override
	public void saveStructure(StructureData entity) {
		logger.warn("saving structure data to Mock: "+entity);
		structureData = entity;
	}


	public void prepareRCPStructure(){
		logger.debug("preparing MOCK RaceCultureProfession structure");

		structureData.setName("mock_structure");
		structureData.setUuid(UUID.fromString("6d63bb71-8894-4039-9f5f-7102ada2b8b7"));

		RaceDSA mittel 	= MockRCPRaceSupplier.getInstance().getRaceByName(MockRCPRaceSupplier.MITTELLAENDER);
//		RaceDSA tulam 	= providerRaces.getRaceByName(ProviderRace.TULAMIDEN);
//		RaceDSA auelf 	= providerRaces.getRaceByName(ProviderRace.AUELFEN);
//		RaceDSA nivese 	= providerRaces.getRaceByName(ProviderRace.NIVESEN);


		CultureDSA mittelStadt 		= MockRCPCultureSupplier.getInstance().getCultureByName(MockRCPCultureSupplier.MITTELLAENDER_STAEDTE);
//		CultureDSA mittelLand 		= providerCultures.getCultureByName(ProviderCulture.MITTELLAENDER_LAND);
//		CultureDSA suedaventurien 	= providerCultures.getCultureByName(ProviderCulture.SUEDAVENTURIEN);
//		CultureDSA elfSiedlung		= providerCultures.getCultureByName(ProviderCulture.ELFISCHE_SIEDLUNG);
//		CultureDSA nivesenStamm 	= providerCultures.getCultureByName(ProviderCulture.NIVESEN_STAEMME);

		ProfessionDSA fuhrmann	 	= MockRCPProfessionSupplier.getInstance().getProfessionByName(MockRCPProfessionSupplier.PROFESSION_FUHRMANN);
		ProfessionDSA taugenichts 	= MockRCPProfessionSupplier.getInstance().getProfessionByName(MockRCPProfessionSupplier.PROFESSION_TAUGENICHTS);
//		ProfessionDSA alchem 	= providerProfessions.getProfessionByName(ProviderProfession.ALCHEMIST);
//		ProfessionDSA bauer 	= providerProfessions.getProfessionByName(ProviderProfession.BAUER);



		//culture lists
		EntityWeightedList mittellaenderCultures = new EntityWeightedList();
		UUIDReference mittelStadtRef = UUIDReference.extractUUIDReference(mittelStadt);
		mittelStadtRef.setUuid(UUID.fromString("4b266dea-ad83-4f47-8595-c7d784dbf51b"));
		EntityWeighted mittelStadtWeighted = new EntityWeighted(mittelStadtRef,IServiceDSAProviderRCPStructure.USUAL_CULTURE_WEIGHT);
		mittelStadtWeighted.setUuid(UUID.fromString("f3a0de28-bc0f-435a-b6ff-dc55c949bf1d"));
		mittellaenderCultures.add(mittelStadtWeighted);
//		mittelUsual.add(mittelLand);
//		mittelUsual.add(suedaventurien);

//		List<CultureDSA> tulamPossible = new ArrayList<CultureDSA>();
//		tulamPossible.add(mittelStadt);

//		List<CultureDSA> niveseUsual = new ArrayList<CultureDSA>();
//		niveseUsual.add(nivesenStamm);

//		List<CultureDSA> nivesePossible = new ArrayList<CultureDSA>();
//		nivesePossible.add(mittelStadt);

//		List<CultureDSA> auelfUsual = new ArrayList<CultureDSA>();
//		auelfUsual.add(elfSiedlung);
		//weave cultures and races
		UUIDReference mittellaenderUUID = UUIDReference.extractUUIDReference(mittel);
		mittellaenderUUID.setUuid(UUID.fromString("bfc29ffb-807f-4d0f-9857-d9a5dba02553"));
		structureData.addCulturesToRace(mittellaenderUUID, mittellaenderCultures);

//		addUsualCulturesToRace(nivese, niveseUsual);
//		addPossibleCulturesToRace(nivese, nivesePossible);

//		addPossibleCulturesToRace(tulam, tulamPossible);

//		addUsualCulturesToRace(auelf, auelfUsual);
		//weave professions to cultures
//		List<ProfessionDSA> suedaventurienProf = new ArrayList<ProfessionDSA>();
//		suedaventurienProf.add(kind);
//		suedaventurienProf.add(alchem);
//		suedaventurienProf.add(bauer);
//		suedaventurienProf.add(fuhrmann);

//		List<ProfessionDSA> nivesenProf = new ArrayList<ProfessionDSA>();
//		nivesenProf.add(kind);
//		nivesenProf.add(alchem);
//		nivesenProf.add(bauer);
//		nivesenProf.add(fuhrmann);

		EntityWeightedList mittel_stadt_Prof = new EntityWeightedList();
		UUIDReference fuhrmannRef = UUIDReference.extractUUIDReference(fuhrmann);
		fuhrmannRef.setUuid(UUID.fromString("7cd1a89e-f331-4bf6-81a4-d2c67cefb78b"));
		EntityWeighted mittelStadtProfWeighted = new EntityWeighted(fuhrmannRef,1D);
		mittelStadtProfWeighted.setUuid(UUID.fromString("c60c2ce2-b25b-4908-9404-6efd326f0df2"));
		mittel_stadt_Prof.add(mittelStadtProfWeighted);
		UUIDReference taugenichtsRef = UUIDReference.extractUUIDReference(taugenichts);
		taugenichtsRef.setUuid(UUID.fromString("919ad298-125e-4f84-a0c2-4311218fa0dc"));
		mittelStadtProfWeighted = new EntityWeighted(taugenichtsRef,1D);
		mittelStadtProfWeighted.setUuid(UUID.fromString("c63f9a7b-70a1-4dc6-9527-9ef18ef4626f"));
		mittel_stadt_Prof.add(mittelStadtProfWeighted);
//		mittel_stadt_Prof.add(alchem);
//		mittel_stadt_Prof.add(bauer);
//		mittel_stadt_Prof.add(fuhrmann);

//		addProfessionsToCulture(suedaventurien, suedaventurienProf);
//		addProfessionsToCulture(nivesenStamm, nivesenProf);
		structureData.addProfessionsToCulture(UUIDReference.extractUUIDReference(mittelStadt), mittel_stadt_Prof);

	}



	public StructureData getStructureData() {
		return structureData;
	}

	@Override
	public void tidyUpStructures(List<UUID> uuids) {
		// nothing to do
	}
}
